﻿using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace firstCodeFirstApp
{
    public class DonatorMap : EntityTypeConfiguration<Donator>
    {
        public DonatorMap()
        {
            ToTable("Donators");
            Property(m => m.Name)
                .IsRequired();
            HasMany(d => d.PayWays)
                .WithRequired()
                .HasForeignKey(p => p.DonatorId);
        }
    }

    public class DonatorTypeMap : EntityTypeConfiguration<DonatorType>
    {
        public DonatorTypeMap()
        {
            HasMany(d => d.Donators)
                .WithOptional(d => d.DonatorType) //外键约束可以为空
                .HasForeignKey(d => d.DonatorTypeId)
                .WillCascadeOnDelete(false);
        }
    }

    public class StudentMap : EntityTypeConfiguration<Student>
    {
        public StudentMap()
        {
            HasRequired(s => s.Person)
                .WithOptional(p => p.Student); // Optional 可选的

            HasKey(s => s.PersonId); //使用 HasKey 方法 指定一个表的主键

            Property(s => s.CollegeName)
                .HasMaxLength(50)
                .IsRequired();
        }
    }

    public class PersonMap : EntityTypeConfiguration<Person>
    {
        public PersonMap()
        {
            /*
             *    * MapLeftKey 配置左外键的列名。左外键指向在 HasMany 调用中指定的导航属性的父实体。
             * MapRightKey 配置右外键的列名。右外键指向在 WithMany 调用中指定的导航属性的父实体。
             */


            HasMany(p => p.Companies)
                .WithMany(c => c.Persons)
                .Map(m => {
                    m.MapLeftKey("PersonId");
                    m.MapRightKey("CompanyId");
                });
        }
    }
}
